package model;

import entity.ClassRoomBlock;
import entity.Subject;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Noi
 */
public class SubjectTableModel extends AbstractTableModel {

    private manager.Manager manager;

    public SubjectTableModel(manager.Manager manager) {
        this.manager = manager;
    }

    @Override
    public int getRowCount() {
        int count = 0;
        for (int i = 0; i < this.manager.getSubjectManager().size(); i++) {
            if (this.manager.getSubjectManager().get(i).isStatus()) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ClassRoomBlock classRoomBlock = null;
        Subject subject = this.manager.getSubjectManager().get(rowIndex);

        if (subject.isStatus() == true) {
            classRoomBlock = this.manager.getClassRoomBlockManager().getById(subject.getClassRoomBlockID());

            switch (columnIndex) {
                case 0:
                    return subject.getSubjectID();
                case 1:
                    return subject.getSubjectName();
                case 2:
                    return classRoomBlock;
                case 3:
                    return subject.getClassPeriod();
                default:
                    return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Mã Môn Học";
            case 1:
                return "Tên Môn Học";
            case 2:
                return "Khối Lớp";
            case 3:
                return "Số Tiết";
            default:
                return null;
        }
    }

    public void add(Subject obj) {
        this.manager.getSubjectManager().add(obj);
        this.fireTableDataChanged();
    }

    public void replace(int index, Subject obj) {
        this.manager.getSubjectManager().replace(index, obj);
        this.fireTableDataChanged();
    }
    
    public void refresh() {
        this.fireTableDataChanged();
    }

    public void remove(Subject obj) {
        this.manager.getSubjectManager().setVisible(obj, false);
        this.fireTableDataChanged();
    }

    public Subject get(int i) {
        int j = 0;
        for (int k = 0; k < this.manager.getSubjectManager().size(); k++) {
            if (this.manager.getSubjectManager().get(k).isStatus()) {
                if (i == j) {
                    return this.manager.getSubjectManager().get(k);
                }
                j++;
            }
        }
        return null;
    }
}